-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format tables including full width characters properly #919
Format tables including full width characters properly #919
Conversation
Use `Unicode.width` to calculate table cell/column width if `unicode` library is loaded.
require 'cucumber/formatter/pretty' | ||
require 'cucumber/cli/options' | ||
|
||
unless Cucumber::JRUBY |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this logic? Does the unicode gem not work for JRuby?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Unicode.width
raises NotImplementedError
with JRuby.
@soutaro can you explain why we need the |
The reason is mainly for compatibility with terminal apps. At least, Terminal.app in Mac OS X have an option to control that. (I'm using with the default setting, disabled.) (I'm not sure how many people are using the option. It may be okay to remove that...) |
@@ -30,6 +30,7 @@ Gem::Specification.new do |s| | |||
s.add_development_dependency 'coveralls', '~> 0.7' | |||
s.add_development_dependency 'syntax', '>= 1.0.0' | |||
s.add_development_dependency 'pry' | |||
s.add_development_dependency 'unicode' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering why this is a development dependency. Won't we need it at runtime too? Or is the idea that we'll expect users to have to have installed it themselves if they want to benefit from it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @soutaro,
I'm terribly sorry it's taken so long to give you feedback on this, and I'm really grateful to you for sharing your code with us.
I took the time this week to try and merge your PR, and I realised it was going to take more work than I had time for in that session. So I've written up what I would have done myself as review comments. If you (or anyone else reading!) wants to pick up those review comments and action them, I think this would be ready to merge.
Essentially it boils down to:
- Using the global config setting rather than using the
Configuration
instance which is where all other options are stored. - A bit of duplication between the pretty formatter and the data table's Cells class
- A puzzle about whether unicode should be a development dependency or not
@@ -9,6 +9,7 @@ | |||
module Cucumber | |||
class << self | |||
attr_accessor :wants_to_quit | |||
attr_accessor :treats_ambiguous_as_fullwidth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having this option is fine, but we need to add it to https://github.com/cucumber/cucumber-ruby/blob/master/lib/cucumber/configuration.rb instead.
This has implications for how we use it in the DataTable
, but we can figure that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also suggest we rename it to treat_ambiguous_width_characters_as_double_width
to be crystal clear.
@@ -692,7 +692,18 @@ def index | |||
end | |||
|
|||
def width | |||
map{|cell| cell.value ? escape_cell(cell.value.to_s).unpack('U*').length : 0}.max | |||
map {|cell| | |||
if cell.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's extract a Cell#width
method here and call it.
if cell.value | ||
escaped = escape_cell(cell.value.to_s) | ||
if defined?(Unicode) | ||
Unicode.width(escaped, Cucumber.treats_ambiguous_as_fullwidth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using the global option here, let's accept this specific option as a constructor parameter (using an options hash or keyword argument would be best IMO) into the DataTable
and pass it down to the Cell
.
Wherever we construct instances of DataTable
we'll almost certainly have access to an instance of the configuration
where we can read that setting from.
@@ -203,7 +203,8 @@ def table_cell_value(value, status) | |||
status ||= @status || :passed | |||
width = @table.col_width(@col_index) | |||
cell_text = escape_cell(value.to_s || '') | |||
padded = cell_text + (' ' * (width - cell_text.unpack('U*').length)) | |||
text_width = defined?(Unicode) ? Unicode.width(cell_text, Cucumber.treats_ambiguous_as_fullwidth) : cell_text.unpack('U*').length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code looks quite similar to what's in DataTable::Cells#width
below. If we move that logic into a Cell#width
method, could we re-use that logic here by constructing an instance of a Cell
?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in a week if no further activity occurs. |
This issue has been automatically closed because of inactivity. You can support the Cucumber core team on opencollective. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR is to format tables including fullwidth characters properly based on Unicode East_Asian_Width informative property.
Pretty formatter does not print tables including fullwidth characters properly as discusses in #251. It prints as
where we want
This is because the text width is calculated by number of Unicode code points and does not consider character width at all. This patch uses
unicode
gem to calculate text width if the library is loaded. (I am not very sure this improvement matters to have another dependency.)The
Cucumber.treats_ambiguous_as_fullwidth
options is to control the width of ambiguous characters likeø
. I don't think it is the best place to have that option.